package com.ncucoder.soms.controller;

import com.ncucoder.soms.dto.ResponseDTO;
import com.ncucoder.soms.entity.Order;
import com.ncucoder.soms.entity.User;
import com.ncucoder.soms.service.OrderService;
import com.ncucoder.soms.service.UserService;
import com.ncucoder.soms.util.JwtTokenUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单控制器
 *
 * 包括: 分页获取订单, 创建订单, 模糊查询订单, 多条件查询
 *
 *
 * @author <a href="https://www.ncucoder.com">hsowan</a>
 * @date 2019-07-15
 **/
@RestController
@RequestMapping("/api/v1/order")
public class OrderController {

    private final OrderService orderService;

    private final UserService userService;

    public OrderController(OrderService orderService, UserService userService) {
        this.orderService = orderService;
        this.userService = userService;
    }


    @GetMapping
    @ApiOperation("分页获取订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "per_page", value = "每页的数量，最大为 100, 默认为 10", defaultValue = "10", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "当前的页码", required = true, dataType = "Integer")
    })
    public List<Order> listOrders(@RequestParam(value = "per_page", required = false) Integer perPage, @RequestParam Integer page, HttpServletResponse response) {
        int maxPerPage = 100;

        if (perPage == null) {
            // 默认每页展示10个订单
            perPage = 10;
        }

        if (perPage > maxPerPage) {
            response.setStatus(ResponseDTO.BAD_REQUEST);
            return null;
        }

        return orderService.listOrders(perPage, page);
    }

}
